package com.gzc.pm.controls
{
	import com.gzc.pm.resource.MyResource;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	public class CPLAxle extends UIComponent
	{
		
		public function CPLAxle()
		{
			_axleBitmap = MyResource.imageDict['axle'];
			
			width = _axleBitmap.width + 200;
			height = _axleBitmap.height + 200;
		}
		
		private var _axleBitmap:Bitmap;
		private var _axleUI:UIComponent;
		private var _drawUI:Canvas;
		
		private var _origX:Number = NaN;
		private var _origY:Number = NaN;
		private var _newX:Number = NaN;
		private var _newY:Number = NaN;
		
		private var _lines:Array = [];
		public function set lines(ac:ArrayCollection):void{
			_lines = ac.toArray();
			repaint();
		}
		public function get lines():ArrayCollection{
			return new ArrayCollection(_lines);
		}
		
		private var _drawable:Boolean;
		public function set drawable(b:Boolean):void{
			_drawable = b;
		}
		
		override protected function createChildren():void{
			_axleUI = new UIComponent();
			_axleUI.width = _axleBitmap.width;
			_axleUI.height = _axleBitmap.height;
			_axleUI.x = (width-_axleUI.width)/2;
			_axleUI.y = (height-_axleUI.height)/2;
			_axleUI.graphics.beginBitmapFill(_axleBitmap.bitmapData);
			_axleUI.graphics.drawRect(0, 0, _axleBitmap.width, _axleBitmap.height);
			_axleUI.graphics.endFill();
			addChild(_axleUI);
			
			
			_drawUI = new Canvas();
			_drawUI.width = width;
			_drawUI.height = height;
			_drawUI.x = 0;
			_drawUI.y = 0;
			addChild(_drawUI);
			
			repaint();
			
			if(_drawable){
				graphics.lineStyle(2, 0x000000, 1);
				graphics.beginFill(0xffffff, 1);
				graphics.drawRoundRect(0, 0, width, height, 10, 10);
				graphics.endFill();
				
				_drawUI.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
		}
		
		private function repaint():void{
			clearUI();
			
			_drawUI.graphics.beginFill(0xffffff, 0);
			_drawUI.graphics.drawRect(0, 0, _drawUI.width, _drawUI.height);
			_drawUI.graphics.endFill();
			
			//绘制已有线条
			_drawUI.graphics.lineStyle(1, 0x000000, 1);
			for each(var line:Object in _lines){
				_drawUI.graphics.moveTo(line.startX, line.startY);
				_drawUI.graphics.lineTo(line.endX, line.endY);
				//显示加工对象名称
				if(line.name){
					var label:CPLLineNameEditor = new CPLLineNameEditor();
					label.lineName = line.name;
					label.editMode = false;
					label.x = calBestX(line.startX, line.endX, label.width, _drawUI.width);
					label.y = calBestY(line.startY, line.endY, label.height, _drawUI.height);;
					_drawUI.addChild(label);
				}
			}
			
			if(!isNaN(_origX) && !isNaN(_origY) && !isNaN(_newX) && !isNaN(_newY)){
//				trace('_newX:'+_newX+', _newY:'+_newY);
				_drawUI.graphics.moveTo(_origX, _origY);
				_drawUI.graphics.lineTo(_newX, _newY);
			}
		}
		
		private function onMouseDown(e:MouseEvent):void{
			if(e.target != _drawUI) return;
			
			_origX = e.localX;
			_origY = e.localY;
//			trace('_origX:'+_origX+', _origX:'+_origX);
			
			_drawUI.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_drawUI.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseMove(e:MouseEvent):void{
			_newX = e.localX;
			_newY = e.localY;
			repaint();
		}
		
		private function onMouseUp(e:MouseEvent):void{
			_drawUI.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			_drawUI.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			_newX = e.localX;
			_newY = e.localY;
			
			if(_origX==_newX && _origY==_newY) return; //打点，未划线
			
			editLineName();
		}
		
		/**
		 * 编辑加工对象名称
		 */
		private function editLineName():void{
			var startX:Number = _origX;
			var startY:Number = _origY;
			var endX:Number = _newX;
			var endY:Number = _newY;
			
			var editor:CPLLineNameEditor = new CPLLineNameEditor();
			editor.editMode = true;
			editor.x = calBestX(startX, endX, editor.width, _drawUI.width);
			editor.y = calBestY(startY, endY, editor.height, _drawUI.height);
			editor.addEventListener(CPLLineNameEditor.EDIT_COMPLETE, onEditComplete);
			editor.addEventListener(CPLLineNameEditor.EDIT_CANCEL, onEditCancel);
			_drawUI.addChild(editor);
			
			function onEditComplete(e:Event):void{
				_lines.push({startX:startX, startY:startY, endX:endX, endY:endY, name:editor.lineName});
				_origX = NaN;
				_origY = NaN;
				_newX = NaN;
				_newY = NaN;
				
				repaint();
			}
			function onEditCancel(e:Event):void{
				_origX = NaN;
				_origY = NaN;
				_newX = NaN;
				_newY = NaN;
				
				repaint();
			}
		}
		
		/**
		 * 计算最佳显示位置
		 */
		private function calBestX(startX:Number, endX:Number, uiWidth:Number, parentWidth:Number):Number{
			var returnX:Number = endX-uiWidth/2;
			var maxX:Number = parentWidth-uiWidth;
			if(returnX > maxX){
				returnX = maxX;
			}
			if(returnX < 0){
				returnX = 0;
			}
			return returnX;
		}
		private function calBestY(startY:Number, endY:Number, uiHeight:Number, parentHeight:Number):Number{
			var returnY:Number = (startY>endY) ? (endY-uiHeight) : endY;
			var maxY:Number = parentHeight-uiHeight;
			if(returnY > maxY){
				returnY = maxY;
			}
			if(returnY < 0){
				returnY = 0;
			}
			return returnY;
		}
		
		/**
		 * 清除界面
		 */
		public function clearUI():void{
			_drawUI.removeAllChildren();
			_drawUI.graphics.clear();
		}
		
		/**
		 * 初始化
		 */
		public function clear():void{
			clearUI();
			_lines = [];
		}
	}
}